package com.headStorm;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.PathUtil;
import cn.hutool.core.io.watch.WatchMonitor;
import cn.hutool.core.io.watch.Watcher;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.http.Header;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.HttpMethod;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequestWithBody;
import io.dronefleet.mavlink.common.FirmwareVersionType;
import io.dronefleet.mavlink.common.MavCmd;
import io.dronefleet.mavlink.common.MavFrame;
import io.dronefleet.mavlink.common.SysStatus;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo {
    public static void main(String[] args) throws IOException, InterruptedException, UnirestException {

        //我是branch1,我已经做过修改了
        // https://blog.csdn.net/zhu_xun/article/details/19539513
        //test(3);
        //unzipFile();
        //System.out.println(SystemUtil.get(SystemUtil.FILE_SEPARATOR));
        //String s = RuntimeUtil.execForStr("obj23dtiles -i D:\\pinjie\\709_new\\709API\\SWAPI\\290\\635\\textured_model\\odm_textured_model.obj --tileset");
        //RuntimeUtil.exec("obj23dtiles -i D:\\pinjie\\709_new\\709API\\SWAPI\\290\\635\\textured_model\\odm_textured_model.obj --tileset");
//        String cmd = "obj23dtiles -i D:\\pinjie\\709_new\\709API\\SWAPI\\290\\635\\textured_model\\odm_textured_model.obj --tileset";
//        Runtime.getRuntime().exec("cmd /k" + cmd);
  /*      String s = RuntimeUtil.execForStr("cmd /k" + cmd);
        System.out.println(s);*/
        //System.out.println(s);

        //generateQrCode();
//        Map<String, Object> otherParamMap = new HashMap<>();
//        otherParamMap.put("startFlightID", 1);
//        otherParamMap.put("startTaskID", 1);

        //向python后台发送图片拼接请求
//                HttpUtil.createGet(" http://127.0.0.1:9002/startCreateOrthoImage").
//                        form(otherParamMap).
//                        executeAsync().get;
        //System.out.println(jsonStr);
//
//
//            WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY);
//            watchMonitor.setWatcher(new Watcher(){
//                @Override
//                public void onCreate(WatchEvent<?> event, Path currentPath) {
//                    Object obj = event.context();
//                    Console.log("创建：{}-> {}", currentPath, obj);
//                }
//
//                @Override
//                public void onModify(WatchEvent<?> event, Path currentPath) {
//                    Object obj = event.context();
//                    Console.log("修改：{}-> {}", currentPath, obj);
//                }
//
//                @Override
//                public void onDelete(WatchEvent<?> event, Path currentPath) {
//                    Object obj = event.context();
//                    Console.log("删除：{}-> {}", currentPath, obj);
//                }
//
//                @Override
//                public void onOverflow(WatchEvent<?> event, Path currentPath) {
//                    Object obj = event.context();
//                    Console.log("Overflow：{}-> {}", currentPath, obj);
//                }
//            });
//
//            //设置监听目录的最大深入，目录层级大于制定层级的变更将不被监听，默认只监听当前层级目录
//            watchMonitor.setMaxDepth(3);
//            //启动监听
//            watchMonitor.start();
//
//            Thread.currentThread().sleep(1000);

//        Integer bitByByte = getBitByByteLowToHigh((byte)122, 7);
//        System.out.println(bitByByte);


        //System.out.println(getNumByByteLowToHigh((byte)122, 0, 2));

//        Byte byte1 = new Byte((byte)120);
//        String binaryStr = NumberUtil.getBinaryStr(byte1);
//            String str1 = "微信公众号菜鸟名企梦\n干货多多";
//            System.out.println(str1);
//            System.out.println("----------------------------------------");
//
//            str1 =  "微信公众号菜鸟名企梦\r干货多多";
//            System.out.println(str1);
//            System.out.println("----------------------------------------");
//
//
//            str1 =  "微信公众号菜鸟名企梦\r\n干货多多";
//            System.out.println(str1);
//            System.out.println("----------------------------------------");
//        JSONObject json = new JSONObject();
//        json.put("username", "1332788xxxxxx");
//        String result = HttpUtil.createGet("http://192.168.3.241:8883/api/test/test18")
//                .body(json.toJSONString(),"application/json")
//                .setMethod(Method.GET)
//                .execute()
//                .body();
//        System.out.println(result);

//        Unirest.setTimeouts(0, 0);
//        new HttpRequestWithBody(HttpMethod.GET, "http://127.0.0.1:8883/api/test/test18").
//                header("Content-Type", "application/json").body("{\r\n    \"username\":\"hzangq\"\r\n}").asString();;

        String url = "http://127.0.0.1:8883/api/test/test18";

//        Gson gson = new GsonBuilder().create();
//
//        ReqParamEntity reqParamEntity = new ReqParamEntity();
//
//        reqParamEntity.setUserName("请求参数在请求body中");
//        reqParamEntity.setPassWord("发送Get请求");
//
//        String reqParams = gson.toJson(reqParamEntity);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("username", "aa");
//
//        try {
//            String result1 = sendJsonByGetReq (url, jsonObject.toJSONString(), "utf-8");
//            System.out.println (result1);
//        } catch (Exception e) {
//            e.printStackTrace ( );
//        }
        //generateQrCode();
        //test();
        test2();
}



   public static void test2(){
       List<File> files = FileUtil.loopFiles("D:\\test");
       for (File file : files) {
           System.out.println(FileUtil.getPrefix(file));
       }
   }


    public static void test(Integer...nums){
        System.out.println(nums.length);
    }


    public static String sendJsonByGetReq(String url, String param, String encoding) throws Exception {

        String body = "";

        //创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();

        HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity(url);
        HttpEntity httpEntity = new StringEntity(param, ContentType.APPLICATION_JSON);
        httpGetWithEntity.setEntity(httpEntity);

        //执行请求操作，并拿到结果（同步阻塞）
        CloseableHttpResponse response = client.execute(httpGetWithEntity);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, encoding);
        }
        //释放链接
        response.close();
        return body;
    }


     static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {

        private final static String METHOD_NAME = "GET";

        @Override
        public String getMethod() {
            return METHOD_NAME;
        }

        public HttpGetWithEntity() {
            super();
        }

        public HttpGetWithEntity(final URI uri) {
            super();
            setURI(uri);
        }

        HttpGetWithEntity(final String uri) {
            super();
            setURI(URI.create(uri));
        }

    }



    public static void test(Integer modelType) {

        switch (modelType) {
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                return;
        }
        System.out.println("finish");
    }

    public static void unzipFile() {
        File unzip = ZipUtil.unzip("D:\\all.zip");
    }

    public static void generateQrCode() {
        QrCodeUtil.generate(//
                "the life is happy", //二维码内容
                QrConfig.create(), //附带logo
                FileUtil.file("d:/test.jpg")//写出到的文件
        );
        String decode = QrCodeUtil.decode(new File("d:/捕获.JPG"));
        System.out.println(decode);

//        boolean hexNumber = HexUtil.isHexNumber("31H");
//        System.out.println(hexNumber);
//        JSONObject drone_switch_task = new JSONObject();
//        JSONObject param = new JSONObject();
//        param.put("id",1);
//        param.put("taskType",1);
//        drone_switch_task.put("cmd","drone_switch_task");
//        drone_switch_task.put("param", param);
//        System.out.println(drone_switch_task.toJSONString());
//
//        JSONObject drone_upload_task = new JSONObject();
//        JSONObject param2 = new JSONObject();
//
//        List<JSONObject> droneList = new ArrayList<>();
//        JSONObject d1 = new JSONObject();
//        d1.put("id",1);
//        d1.put("id",1);
//
//
//        JSONObject d2 = new JSONObject();
//
//
//        param.put("id",1);
//        drone_upload_task.put("cmd","drone_upload_task");
//        drone_upload_task.put("param", param);
//        System.out.println(drone_upload_task.toJSONString());

    }


    /**
     * 把单个字节转换成二进制字符串
     */
    public static String byteToBin(byte b) {
        String zero = "00000000";
        String binStr = Integer.toBinaryString(b & 0xFF);
        if (binStr.length() < 8) {
            binStr = zero.substring(0, 8 - binStr.length()) + binStr;
        }
        System.out.println(binStr);
        return binStr;
    }

    /**
     * 获取字节在内存中某一位的值,采用字符取值方式,下标的顺序从最高位到最低位
     */
    public static Integer getBitByByteHighToLow(byte b, int index) {
        if (index >= 8) {
            return null;
        }
        Integer val = null;
        String binStr = byteToBin(b);
        val = Integer.parseInt(String.valueOf(binStr.charAt(index)));
        return val;
    }

    /**
     * 获取字节在内存中某一位的值,采用字符取值方式,下标的顺序从最高位到最低位
     */
    public static Integer getBitByByteLowToHigh(byte b, int index) {
        if (index >= 8) {
            return null;
        }
        index = 7 - index;
        Integer val = null;
        String binStr = byteToBin(b);
        val = Integer.parseInt(String.valueOf(binStr.charAt(index)));
        return val;
    }

    public static Integer getNumByByteLowToHigh(byte b, int beginIndex, int endIndex) {
        if (beginIndex >= 8 || endIndex >= 8 || endIndex < beginIndex) {
            return null;
        }
        String binStr1 = "";
        String binStr2 = byteToBin(b);
        for (int index = beginIndex; index < endIndex + 1; index++) {
            binStr1 += String.valueOf(binStr2.charAt(index));
        }
        return Integer.parseInt(binStr1, 2);
    }

}
